package com.aaa.cars.service.impl;

import com.aaa.cars.dao.BaseAreaDao;
import com.aaa.cars.entity.BaseArea;
import com.aaa.cars.service.BaseAreaService;
import com.aaa.common.constants.ReturnStatus;
import com.aaa.common.util.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 统一地区库(BaseArea)表服务实现类
 *
 * @author makejava
 * @since 2020-12-07 20:25:50
 */
@Service("baseAreaService")
public class BaseAreaServiceImpl implements BaseAreaService {
    @Resource
    private BaseAreaDao baseAreaDao;


    /**
     * 通过实体作为筛选条件查询
     *
     * @param baseArea 实例对象
     * @return 对象列表
     */
    @Override
    public Result queryAll(BaseArea baseArea) {
        return new Result(ReturnStatus.SUCCESS.getReturnCode(),
                ReturnStatus.SUCCESS.getReturnMsg(), this.baseAreaDao.queryAll(baseArea));
    }

    @Override
    public Result queryAllByBaseAreaid(Integer baseAreaid) {
        return new Result(ReturnStatus.SUCCESS.getReturnCode() ,
                ReturnStatus.SUCCESS.getReturnMsg() , this.baseAreaDao.queryAllByBaseAreaid(baseAreaid));
    }

    @Override
    public Result queryAllByParentid(Integer parentid) {
        return new Result(ReturnStatus.SUCCESS.getReturnCode() ,
                ReturnStatus.SUCCESS.getReturnMsg() , this.baseAreaDao.queryAllByParentid(parentid));
    }

    @Override
    public Result queryAllCity() {
        List<BaseArea> baseAreas = baseAreaDao.queryAllByParentid(0);
        List<BaseArea> cityList = new ArrayList<>();
        if (baseAreas != null && baseAreas.size() > 0) {
            for (BaseArea baseArea : baseAreas) {
                List<BaseArea> baseAreas1 = baseAreaDao.queryAllByParentid((Integer) baseArea.getBaseAreaid());
                for (BaseArea area : baseAreas1) {
                    cityList.add(area);
                }
            }
        }
        return new Result(ReturnStatus.SUCCESS.getReturnCode() ,
                ReturnStatus.SUCCESS.getReturnMsg() , cityList);
    }

    @Override
    public Result queryAllArea() {
        List<BaseArea> baseAreas = baseAreaDao.queryAllByParentid(0);
        List<BaseArea> cityList = new ArrayList<>();
        if (baseAreas != null && baseAreas.size() > 0) {
            for (BaseArea baseArea : baseAreas) {
                List<BaseArea> baseAreas1 = baseAreaDao.queryAllByParentid((Integer) baseArea.getBaseAreaid());
                for (BaseArea area : baseAreas1) {
                    List<BaseArea> baseAreas2 = baseAreaDao.queryAllByParentid((Integer) area.getBaseAreaid());
                    for (BaseArea baseArea1 : baseAreas2) {
                        cityList.add(baseArea1);
                    }
                }
            }
        }
        return new Result(ReturnStatus.SUCCESS.getReturnCode() ,
                ReturnStatus.SUCCESS.getReturnMsg() , cityList);
    }
}